<template>
    <div class="container">
        <Form :model="searchObj" inline>
            <FormItem>
                <Input v-model="searchObj.experts_name" class="search-input" clearable placeholder="请输入姓名"/>
            </FormItem>
            <FormItem>
                <Button class="search-btn" type="success" @click="init">
                    <Icon type="ios-search"/>
                    搜索
                </Button>
            </FormItem>
            <FormItem>
                <Button class="search-btn" type="primary" @click="addBtn">
                    <Icon type="ios-add"/>
                    添加
                </Button>
            </FormItem>
        </Form>
        <Row type="flex" justify="center">
            <Col span="24">
                <Table border :loading="tableLoading" :columns="tableColumns" :data="tableData"
                       @on-selection-change="tableSelectChange">
                    <template slot="action" slot-scope="{ row }">
                        <Button type="primary" size="small" class="action-btn" @click="editBtn(row)">编辑</Button>
                        <Button type="info" size="small" class="action-btn" @click="infoBtn(row)">详情</Button>
                        <Button type="error" size="small" class="action-btn" @click="delBtn(row.card_no)">删除</Button>
                    </template>
                </Table>
            </Col>
        </Row>
        <br>
        <option-modal ref="optionModal" :modal-title="modalTitle" :modal-show="modalShow" :modal-type="modalType"
                      :dataObj="modalData" @cancelOption="cancelOption" @saveOption="saveOption"></option-modal>
    </div>
</template>

<script>
import {
  searchConsultation,
  updateReviseState
} from '@/api/customer-service'
import OptionModal from './components/option-modal'

export default {
  components: { OptionModal },
  data () {
    return {
      modalShow: false,
      modalTitle: '',
      modalType: '',
      modalData: {},
      tableColumns: [
        {
          type: 'index',
          width: 70,
          title: '序号',
          align: 'center'
        },
        {
          title: '专家姓名',
          key: 'experts_name',
          width: 100,
          align: 'center'
        },
        {
          title: '咨询时间',
          key: 'experts_education',
          width: 200,
          align: 'center'
        },
        {
          title: '开办单位',
          key: 'company_name',
          width: 240,
          align: 'center'
        },
        {
          title: '电话号码',
          key: 'experts_telephone_number',
          width: 170,
          align: 'center'
        },
        {
          title: '操作',
          minWidth: 200,
          slot: 'action',
          align: 'center'
        }
      ],
      tableData: [],
      selectData: [],
      tableLoading: false,
      searchObj: {}
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    cancelOption () {
      this.modalShow = false
    },
    saveOption () {
      this.modalShow = false
      this.init()
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    },
    tableSelectChange (val) {
      this.selectData = val
    },
    init () {
      this.tableLoading = true
      let data = { experts_name: this.$common.xeUtils.trim(this.searchObj.experts_name) }
      searchConsultation(data).then(ret => {
        if (ret.data.errcode == 200) {
          this.tableLoading = false
          this.tableData = ret.data.data
        } else {
          this.tableLoading = false
          this.error(data)
        }
      })
    },
    addBtn () {
      this.modalTitle = '添加'
      this.modalType = 'add'
      this.modalData = {}
      this.modalShow = true
    },
    editBtn (val) {
      this.modalTitle = '编辑'
      this.modalType = 'edit'
      this.modalData = this.$common.xeUtils.clone(val)
      this.modalShow = true
    },
    infoBtn (val) {
      this.modalTitle = '详情'
      this.modalType = 'info'
      this.modalData = this.$common.xeUtils.clone(val)
      this.modalShow = true
    },
    delBtn (val) {
      this.$Modal.confirm({
        title: '确认删除此人么？',
        content: '请仔细核对后再操作',
        loading: true,
        onOk: () => {
          this.deleteOne(val)
        }
      })
    },
    deleteOne (val) {
      updateReviseState({ card_no: val }).then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.$Modal.remove()
          this.init()
          this.success('删除成功')
        } else {
          this.$Modal.remove()
          this.error(data)
        }
      })
    }
  }

}
</script>

<style scoped>
    .container {
        padding-bottom: 50px;
    }

    .action-btn {
        margin-right: 5px;
        margin-bottom: 5px;
    }

    .search-input {
        width: 180px;
    }

    .search-btn {
        width: 100px;
    }
</style>
